// pages/withdrawal/withdrawal.js
import {
  request
} from '../../request/index.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    money: '',
    showPayPwdInput: false, //是否展示密码输入层
    pwdVal: '', //输入的密码
    payFocus: true, //文本框焦点
    moneyFee: 0,
    showmoney: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    let user = typeof wx.getStorageSync('user') === 'string' ? JSON.parse(wx.getStorageSync('user')) : wx.getStorageSync('user');
    this.setData({
      user,
    });
  },


  //获取输入提现金额绑定的值
  bindKeyInput2(e) {
    let money = parseFloat(e.detail.value);
    let showmoney = money.toFixed(2);
    this.setData({
      money,
      showmoney
    })
  },
  //提现完成之后点击确定触发的函数
  submit() {},

  /**
   * 点击确定显示支付密码输入层
   */
  showInputLayer: function () {
    let reg = /\D/;
    let user = this.data.user;
    if (this.data.money == 0) {
      this.hidePayLayer('输入的金额为0', 'none');
    } else if (this.data.money === '') {
      this.hidePayLayer('输入的金额不能为空', 'none');
    } else if (reg.test(this.data.money)) {
      this.hidePayLayer('必须输入数字', 'none');
    } else if (this.data.money > this.data.user.safetyInfo.account_money) {
      this.hidePayLayer('已超出可提现金额', 'none');
    } else if (user.safetyInfo.pay_password == null || this.data.user.safetyInfo.pay_password == '') {
      wx.navigateTo({
        url: '../../pages/setPass/setPass'
      })
    } else {
      request({
        url: 'WithdrawDeposit/getWithdrawalFee',
        data: {
          open_id: this.data.user.safetyInfo.open_id,
          account_money: this.data.money
        }
      }).then(r => {
        console.log(r);
        if(this.data.money == this.data.user.safetyInfo.account_money){
          let showmoney=(this.data.money)-parseFloat(this.data.money * r.data.data.withdrawal_fee);
          console.log(showmoney);
          this.setData({
            showmoney
          })
        }
        this.setData({
          moneyFee: r.data.data.withdrawal_fee,
          showPayPwdInput: true,
          payFocus: true
        })
      })
    }

  },

  // 点击返回、忘记密码触发的函数
  hidePayLayer1() {
    this.hidePayLayer('取消支付', 'none')
  },
  /**
   * 隐藏支付密码输入层
   */
  hidePayLayer(text, icon) {

    this.setData({
        showPayPwdInput: false,
        payFocus: false,
        pwdVal: ''
      },
      function () {
        wx.showToast({
          title: text,
          icon: icon
        })
      });

  },
  /**
   * 获取焦点
   */
  getFocus() {
    this.setData({
      payFocus: true
    });
  },
  /**
   * 输入密码监听
   */
  inputPwd(e) {
    this.setData({
      pwdVal: e.detail.value
    });
    if (e.detail.value.length >= 6 && this.data.user.safetyInfo.pay_password == e.detail.value) {
      this.hidePayLayer('支付成功', 'success');
      let totalmoney = 0;
      if (this.data.money == this.data.user.safetyInfo.account_money) {
        totalmoney = (this.data.money);
      } else {
        totalmoney = (this.data.money) + parseFloat(this.data.money * this.data.moneyFee);
      }
      request({
        url: 'safetyInfo/upDaccountMoney',
        data: {
          open_id: this.data.user.safetyInfo.open_id,
          account_money: totalmoney
        },
      }).then((result) => {
        console.log(result);
        if (result.data.code == 0) {
          this.hidePayLayer('提现1-3个工作日内到账', 'none');
          setTimeout(() => {
            this.setData({
              user: result.data.data
            });
            wx.setStorageSync('user', result.data.data);
            wx.navigateBack({
              delta: 1,
            })
          }, 1000)
        }
      });

    } else if (e.detail.value.length >= 6 && this.data.user.safetyInfo.pay_password != e.detail.value) {
      this.hidePayLayer('密码输入错误', 'none');
    }
  },
  goPass() {
    wx.navigateTo({
      url: '../editPass/editPass',
    })
  }
})